//
//  LLQRcodeViewController.swift
//  新印象
//
//  Created by wyl on 2016/12/30.
//  Copyright © 2016年 魏延龙. All rights reserved.
//

import UIKit
import AVFoundation

class LLQRcodeViewController: UIViewController ,AVCaptureMetadataOutputObjectsDelegate{

    let leftProportion : CGFloat = 0.2
    let topProportion : CGFloat = 0.3
    let widthProportion : CGFloat = 0.6
    // 保证 2*leftProportion + widthProportion = 1
    var captureSession : AVCaptureSession?
    var videoPreviewLayer : AVCaptureVideoPreviewLayer?
    var scanLine = UIImageView()
    var timer = Timer()
    var barcodeView : UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.navigationController?.navigationBar.isHidden = true
        self.view.backgroundColor = .white
        
        
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let input = try? AVCaptureDeviceInput.init(device: captureDevice)
        captureSession = AVCaptureSession()
        captureSession?.addInput(input)
        let output = AVCaptureMetadataOutput()
        captureSession?.addOutput(output)
        output.setMetadataObjectsDelegate(self, queue: .main)
        output.metadataObjectTypes = [AVMetadataObjectTypeQRCode]
        
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = view.layer.frame
        view.layer.addSublayer(videoPreviewLayer!)
        captureSession?.startRunning()
        output.rectOfInterest = (videoPreviewLayer?.metadataOutputRectOfInterest(for: CGRect(x: SCREENWIDTH * leftProportion, y: SCREENHEIGHT * topProportion, width: SCREENWIDTH * widthProportion, height: SCREENWIDTH * widthProportion)))!//CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
        print(output.rectOfInterest)
        
        barcodeView = UIView(frame: CGRect(x: SCREENWIDTH * leftProportion, y: SCREENHEIGHT * topProportion, width: SCREENWIDTH * widthProportion, height: SCREENWIDTH * widthProportion))
        barcodeView.layer.borderWidth = 1.0
        barcodeView.layer.borderColor = UIColor.white.cgColor
        self.view.addSubview(barcodeView)
        
        scanLine.frame = CGRect(x: 0, y: 0, width: barcodeView.frame.size.width, height: 5)
        scanLine.image = UIImage(named: "qrcode_Scan_weixin_Line")
        barcodeView.addSubview(scanLine)
        timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(moveScannerLayer(_:)), userInfo: nil, repeats: true)
        timer.fire()
        TranslucentView()
        
    }
    //让扫描线滚动
    func moveScannerLayer(_ timer : Timer) {
        scanLine.frame = CGRect(x: 0, y: 0, width: self.barcodeView.frame.size.width, height: 5)
        UIView.animate(withDuration: 2) {
            self.scanLine.frame = CGRect(x: self.scanLine.frame.origin.x, y: self.scanLine.frame.origin.y + self.barcodeView.frame.size.height - 10, width: self.scanLine.frame.size.width, height: self.scanLine.frame.size.height)
        }
    }
    func TranslucentView() {
        let topview = UIView(frame: CGRect(x: 0, y: 0, width: SCREENWIDTH, height: SCREENHEIGHT * topProportion))
        topview.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.4)
//        topview.backgroundColor = .red
        self.view.addSubview(topview)
        
        let leftview = UIView(frame: CGRect(x: 0, y: SCREENHEIGHT * topProportion, width: SCREENWIDTH * leftProportion, height: SCREENWIDTH * widthProportion))//257
        leftview.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.4)
//        leftview.backgroundColor = .orange
        self.view.addSubview(leftview)
        
        let bottomview = UIView(frame: CGRect(x: 0, y: SCREENHEIGHT * topProportion + SCREENWIDTH * widthProportion, width: SCREENWIDTH, height: SCREENHEIGHT - (SCREENHEIGHT * topProportion + SCREENWIDTH * widthProportion)))
        bottomview.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.4)
//        bottomview.backgroundColor = .yellow
        self.view.addSubview(bottomview)
        
        print(1 - leftProportion - widthProportion)
        let rightview = UIView(frame: CGRect(x: SCREENWIDTH * (leftProportion + widthProportion), y: SCREENHEIGHT * topProportion, width: SCREENWIDTH * leftProportion, height: SCREENWIDTH * widthProportion))
        rightview.backgroundColor = UIColor.init(colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.4)
//        rightview.backgroundColor = .blue
        self.view.addSubview(rightview)
    }
    
    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
        
        if metadataObjects == nil || metadataObjects.count == 0 {
            print("无")
            return
        }
        
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
        if metadataObj.type == AVMetadataObjectTypeQRCode {
            if metadataObj.stringValue != nil {
                timer.invalidate()
                captureSession?.stopRunning()
                print("扫描到二维码")
                print(metadataObj.stringValue)
                let result = LLUrlViewController()
                result.url = metadataObj.stringValue
                self.navigationController?.pushViewController(result, animated: true)
                
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

class resultWkController: UIViewController {
    var url : String!
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
}
